package net.mikoo.seals.biz.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;

import net.mikoo.seals.biz.common.BaseServices;
import net.mikoo.seals.biz.manager.VerifyManager;
import net.mikoo.seals.biz.mapper.ApplyVerifyMapper;
import net.mikoo.seals.biz.service.VerifyService;
import net.mikoo.seals.common.exception.ParametersException;
import net.mikoo.seals.common.model.PageModel;
import net.mikoo.seals.model.ApplyVerify;
import net.mikoo.seals.model.ApplyVerifyQuery;

public class VerifyServiceImpl extends BaseServices implements VerifyService {

	
	
	@Autowired
	private ApplyVerifyMapper applyVerifyMapper;
	
	@Autowired
	private VerifyManager verifyManager;
	
	@Override
	public ApplyVerify getVerifyDetail(Long verifyId) {
		
		if(verifyId == null) {
			throw new IllegalArgumentException("verifyId");
		}
		return applyVerifyMapper.selectInfoById(verifyId);
	}
	
	
	@Override
	public void verify(ApplyVerify verify) {
		
		transactionTemplate.execute(new TransactionCallbackWithoutResult() {

			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				try {
					verifyManager.checkParams(verify);
					verifyManager.verify(verify);
				} catch (ParametersException e) {
					status.setRollbackOnly();
					throw e;
				} catch (Exception e) {
					status.setRollbackOnly();
					throw e;
				}

			}
		});
	}

	@Override
	public PageModel<ApplyVerify> getPageByQuery(ApplyVerifyQuery query) {

		List<ApplyVerify> datas = applyVerifyMapper.selectByQueryToPage(query);
		Integer total = applyVerifyMapper.countByQueryToPage(query);

		PageModel<ApplyVerify> pm = new PageModel<>(total, datas);

		return pm;
	}


	@Override
	public ApplyVerify getInfoByQuery(ApplyVerifyQuery query) {
		ApplyVerify verify = applyVerifyMapper.selectInfoByQuery(query);
		return verify;
	}


	@Override
	public List<ApplyVerify> getListByQuery(ApplyVerifyQuery query) {
		
		List<ApplyVerify> verifys = applyVerifyMapper.selectListByQuery(query);
		
		return verifys;
	}
	
}
